


#include <Wt/WPanel>
#include <Wt/WVBoxLayout>
#include <Wt/WHBoxLayout>
#include <Wt/WGridLayout>
#include <Wt/WStackedWidget>
#include <Wt/WAnchor>
#include <Wt/WText>
#include <Wt/WTextEdit>
#include <Wt/WTextArea>
#include <Wt/WPushButton>
#include <Wt/WContainerWidget>
#include <Wt/WLength>
#include <Wt/WTabWidget>
#include <Wt/WDateTime>
#include <Wt/WMessageBox>

#include "PlanWidget.h"

#include "DbSession.h"
#include <QVector>





PlanWidget::PlanWidget(int userId, PlanType planType, WObject *parent, bool other, int guestId)
{
    this->setParent(parent);
    this->userId = userId;
    this->planType = planType;
    this->other = other;
    this->guestId = guestId;

    planPanel = new WContainerWidget();
    stackedWidget = new WStackedWidget(/*planPanel*/);
    showView = new WContainerWidget();
    editView = new WContainerWidget();
    planText = new WText();
    planText->setMinimumSize(600, 50);
    WVBoxLayout *showViewLayout = new WVBoxLayout();
    prevPlan = new WPushButton("Previous plan");
    nextPlan = new WPushButton("Next plan");
    newPlan = new WPushButton("New plan");
    if (other)
        newPlan->hide();
    WHBoxLayout *planButtons = new WHBoxLayout();
    planButtons->addWidget(prevPlan);
    planButtons->addWidget(newPlan);
    planButtons->addWidget(nextPlan);
    planModified = new WText("Modified: ");
    showViewLayout->addItem(planButtons);
    showViewLayout->addWidget(planModified);
    showViewLayout->addWidget(planText);
    showViewLayout->addStretch(1);
    //showViewLayout->addWidget(new WText("Comments:"));
    showView->setLayout(showViewLayout);
    planEdit = new WTextEdit();
    planEdit->setMinimumSize(WLength(600), WLength(300));
    saveEdit = new WPushButton("Save");
    cancelEdit = new WPushButton("Cancel");
    WVBoxLayout *editViewLayout = new WVBoxLayout();
    WHBoxLayout *buttonsLayout = new WHBoxLayout();
    buttonsLayout->addWidget(saveEdit);
    buttonsLayout->addWidget(cancelEdit);
    editViewLayout->addWidget(planEdit);
    editViewLayout->addItem(buttonsLayout);
    editViewLayout->addWidget(new WText("Notice: after saving this plan will be marked as your current plan"));
    editView->setLayout(editViewLayout);

    commentsWidget = new WContainerWidget();
    showViewLayout->addWidget(commentsWidget);
    //commentsLayout = new WGridLayout();

    addCommentButton = new WPushButton("Send");
    addCommentButton->clicked().connect(this, &PlanWidget::onAddCommentClicked);
    addCommentEdit = new WTextEdit();
    addCommentEdit->setMinimumSize(WLength(600), WLength(150));
    WVBoxLayout *addCommentLayout = new WVBoxLayout();
    addCommentLayout->addWidget(new WText("Add comment:"));
    addCommentLayout->addWidget(addCommentEdit);
    addCommentLayout->addWidget(addCommentButton);
    addCommentLayout->addStretch(1);
    addCommentWidget = new WContainerWidget();
    addCommentWidget->setLayout(addCommentLayout);
    showViewLayout->addWidget(addCommentWidget);

    currentPlan = -1;
    update();


    this->addTab(showView, "View plan");
    if (!other)
    {
        this->addTab(editView, "Edit plan");
    }
    //this->addTab(addCommentWidget, "Add comment");

    saveEdit->clicked().connect(this, &PlanWidget::onSaveClicked);
    cancelEdit->clicked().connect(this, &PlanWidget::onCancelClicked);
    prevPlan->clicked().connect(this, &PlanWidget::onPrevClicked);
    nextPlan->clicked().connect(this, &PlanWidget::onNextClicked);
    newPlan->clicked().connect(this, &PlanWidget::onNewClicked);

    setShowView();
}

PlanWidget::~PlanWidget()
{
    delete showView;
    delete editView;

    for (int i = 0; i < comments.count(); i++)
    {
        delete comments.at(i);
    }
}

void PlanWidget::setShowView()
{
    this->setCurrentIndex(0);
}

void PlanWidget::setEditView()
{
    this->setCurrentIndex(1);
}

void PlanWidget::update()
{
    /* Remove current widget with comments from container */
    if (currentPlan >= 0)
    {
        commentsWidget->removeWidget(comments.at(currentPlan));
    }

    amountOfPlans = 0;
    currentPlan = -1;

    /* Free memory allocated for widget with comments */
    for (int i = 0; i < comments.count(); i++)
    {
        delete comments.at(i);
    }

    /* Clear stack to remove pointers to widget with comments */
    while (comments.count() > 0)
        comments.pop_back();

    /* Now lets create widgets with comments for each plan.
       Pointers to widget will be placed in vector */
    if (planType == DIET)
    {
        dietPlans = DbSession::getInstance().getDietPlan(userId);

        /*  For each plan create widget with comments and push it in vector */
        for (int i = 0; i < dietPlans->count(); i++)
        {
            WContainerWidget *widget = new WContainerWidget();

            /* If no comments then write about it */
            if (dietPlans->at(i)->getDietComments()->count() == 0)
                new WText("No comments... yet.", widget);
            else
                new WText("Comments:", widget);


            /* Add in this widget all comments for current plan */
            for (int j = 0; j < dietPlans->at(i)->getDietComments()->count(); j++)
            {
                WPanel *comment = new WPanel(widget);
                comment->setTitle(DbSession::getInstance().getUser(dietPlans->at(i)->
                    getDietComments()->at(j)->getUserId())->getLogin().toStdString());
                comment->setCentralWidget(new WText(WString::fromUTF8(dietPlans->at(i)->
                    getDietComments()->at(j)->getInfo().toUtf8().data())));
            }

            comments.push_back(widget);
            amountOfPlans++;
        }

        if (amountOfPlans > 0)
        {
            prevPlan->show();
            nextPlan->show();
            editView->show();
            addCommentWidget->show();
            commentsWidget->addWidget(comments.last());
            currentPlan = amountOfPlans - 1;
            planText->setText(WString::fromUTF8(dietPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planEdit->setText(WString::fromUTF8(dietPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planModified->setText(WString("Modified: ") + dietPlans->at(currentPlan)->getDate().toString().toStdString());
        }
        else
        {
            prevPlan->hide();
            nextPlan->hide();
            addCommentWidget->hide();
            planModified->setText("User has no diet plans");
            editView->hide();
        }
    }
    else if (planType == FITNESS)
    {
        fitnessPlans = DbSession::getInstance().getFitnessPlan(userId);
        for (int i = 0; i < fitnessPlans->count(); i++)
        {
            WContainerWidget *widget = new WContainerWidget();

            /* If no comments then write about it */
            if (fitnessPlans->at(i)->getFitnessComments()->count() == 0)
                new WText("No comments... yet.", widget);
            else
                new WText("Comments:", widget);

            for (int j = 0; j < fitnessPlans->at(i)->getFitnessComments()->count(); j++)
            {
                WPanel *comment = new WPanel(widget);
                comment->setTitle(DbSession::getInstance().getUser(fitnessPlans->at(i)->
                    getFitnessComments()->at(j)->getUserId())->getLogin().toStdString());
                comment->setCentralWidget(new WText(WString::fromUTF8(fitnessPlans->at(i)->
                    getFitnessComments()->at(j)->getInfo().toUtf8().data())));
            }

            comments.push_back(widget);
            amountOfPlans++;
        }

        if (amountOfPlans > 0)
        {
            prevPlan->show();
            nextPlan->show();
            editView->show();
            addCommentWidget->show();
            commentsWidget->addWidget(comments.last());
            currentPlan = amountOfPlans - 1;
            planText->setText(WString::fromUTF8(fitnessPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planEdit->setText(WString::fromUTF8(fitnessPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planModified->setText(WString("Modified: ") + fitnessPlans->at(currentPlan)->getDate().toString().toStdString());
        }
        else
        {
            prevPlan->hide();
            nextPlan->hide();
            planModified->setText("User has no fitness plans");
            editView->hide();
            addCommentWidget->hide();
        }
    }
}

void PlanWidget::onEditClicked()
{
    setEditView();
}

void PlanWidget::onSaveClicked()
{
    if (planType == DIET)
    {
        DbSession::getInstance().editDietPlan(dietPlans->at(currentPlan)->getDietId(),
                                              QString::fromUtf8(planEdit->text().toUTF8().c_str()));
    }
    else if (planType = FITNESS)
    {
        DbSession::getInstance().editFitnessPlan(fitnessPlans->at(currentPlan)->getFitnessId(),
                                                 QString::fromUtf8(planEdit->text().toUTF8().c_str()));
    }

    update();
    setShowView();
}

void PlanWidget::onCancelClicked()
{
    planEdit->setText(planText->text());
    setShowView();
}

void PlanWidget::onPrevClicked()
{
    if (planType == DIET)
    {
        if (currentPlan > 0)
        {
            commentsWidget->removeWidget(comments.at(currentPlan));
            currentPlan--;
            commentsWidget->addWidget(comments.at(currentPlan));
            planText->setText(WString::fromUTF8(dietPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planEdit->setText(WString::fromUTF8(dietPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planModified->setText(WString("Modified: ") + dietPlans->at(currentPlan)->getDate().toString().toStdString());
        }
    }
    else if (planType == FITNESS)
    {
        if (currentPlan > 0)
        {
            commentsWidget->removeWidget(comments.at(currentPlan));
            currentPlan--;
            commentsWidget->addWidget(comments.at(currentPlan));
            planText->setText(WString::fromUTF8(fitnessPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planEdit->setText(WString::fromUTF8(fitnessPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planModified->setText(WString("Modified: ") + fitnessPlans->at(currentPlan)->getDate().toString().toStdString());
        }
    }
}

void PlanWidget::onNextClicked()
{
    if (planType == DIET)
    {
        if ((currentPlan > -1) && (currentPlan < (amountOfPlans - 1)))
        {
            commentsWidget->removeWidget(comments.at(currentPlan));
            currentPlan++;
            commentsWidget->addWidget(comments.at(currentPlan));
            planText->setText(WString::fromUTF8(dietPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planEdit->setText(WString::fromUTF8(dietPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planModified->setText(WString("Modified: ") + dietPlans->at(currentPlan)->getDate().toString().toStdString());
        }
    }
    if (planType == FITNESS)
    {
        if ((currentPlan > -1) && (currentPlan < (amountOfPlans - 1)))
        {
            commentsWidget->removeWidget(comments.at(currentPlan));
            currentPlan++;
            commentsWidget->addWidget(comments.at(currentPlan));
            planText->setText(WString::fromUTF8(fitnessPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planEdit->setText(WString::fromUTF8(fitnessPlans->at(currentPlan)->getInfo().toUtf8().data()));
            planModified->setText(WString("Modified: ") + fitnessPlans->at(currentPlan)->getDate().toString().toStdString());
        }
    }
}

void PlanWidget::onNewClicked()
{
    if (planType == DIET)
    {
        DbSession::getInstance().addDietPlan(userId,
            "This will be your new diet plan. Good luck!");
    }
    else if (planType = FITNESS)
    {
        DbSession::getInstance().addFitnessPlan(userId,
            "This will be your new fitness plan. Good luck!");
    }

    update();
    setEditView();
}

void PlanWidget::onAddCommentClicked()
{
    if (addCommentEdit->text() == WString(""))
        return;

    if (planType == DIET)
    {
        if (!other)
        {
            DbSession::getInstance().addDietComment(dietPlans->at(currentPlan)->getDietId(),
                                                    userId,
                                                    QString::fromUtf8(addCommentEdit->text().toUTF8().c_str()));
        }
        else
        {
            DbSession::getInstance().addDietComment(dietPlans->at(currentPlan)->getDietId(),
                                                    guestId,
                                                    QString::fromUtf8(addCommentEdit->text().toUTF8().c_str()));
        }
    }
    else if (planType == FITNESS)
    {
        if (!other)
        {
            DbSession::getInstance().addFitnessComment(fitnessPlans->at(currentPlan)->getFitnessId(),
                                                    userId,
                                                    QString::fromUtf8(addCommentEdit->text().toUTF8().c_str()));
        }
        else
        {
            DbSession::getInstance().addFitnessComment(fitnessPlans->at(currentPlan)->getFitnessId(),
                                                    guestId,
                                                    QString::fromUtf8(addCommentEdit->text().toUTF8().c_str()));
        }
    }

    update();
    setShowView();
}




